package com.shawn.server.controller;

import com.shawn.dubbo.api.RecommendUserApi;
import com.shawn.model.dto.RecommendUserDto;
import com.shawn.model.entity.UserInfo;
import com.shawn.model.mongo.RecommendUser;
import com.shawn.model.vo.NearlyUserVo;
import com.shawn.model.vo.PageResult;
import com.shawn.model.vo.TodayBest;
import com.shawn.server.interceptor.ThreadContext;
import com.shawn.server.service.TouchLoveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/tanhua")
public class TouchLoveController {
    @Autowired
    private TouchLoveService touchLoveService;


    /**
     * 搜索附近人
     *
     * @param gender   性别
     * @param distance 距离
     * @return {@link ResponseEntity}
     */
    @GetMapping("/search")
    public ResponseEntity searchNearlyPeople(String gender,String distance){
        Long userId = ThreadContext.getUserId();
       List<NearlyUserVo> list = touchLoveService.searchNearlyPeople(userId,gender,distance);
       return ResponseEntity.ok(list);
    }

    /**
     * 从推荐用户中,推荐探花卡片
     *
     * @return {@link ResponseEntity}
     */
    @GetMapping("/cards")
    public ResponseEntity recommendCards(){
        Long userId = ThreadContext.getUserId();
        List<TodayBest> todayBests = touchLoveService.recommendCards(userId);
        return ResponseEntity.ok(todayBests);
    }

    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable Long id){
        Long userId = ThreadContext.getUserId();
        //将数据保存到喜欢表
        touchLoveService.whetherLoveUser(userId,id,true);
        return ResponseEntity.ok("已喜欢");
    }

    @GetMapping("/{id}/unlove")
    public ResponseEntity unLove(@PathVariable Long id){
        Long userId = ThreadContext.getUserId();
        //将数据保存到喜欢表
        touchLoveService.whetherLoveUser(userId,id,false);
        return ResponseEntity.ok("已取消喜欢");
    }

    @GetMapping("/todayBest")
    public ResponseEntity getTodayLove(){
        Long userId = ThreadContext.getUserId();
        TodayBest todayBest = touchLoveService.getTodayLove(userId);
        log.info("获取到今日佳人:{}",todayBest.toString());
        return ResponseEntity.ok(todayBest);
    }

    /**
     * 推荐用户
     *
     * @param recommendUserDto 建议用户dto
     * @return {@link ResponseEntity}
     */
    @GetMapping("/recommendation")
    public ResponseEntity recommendation(RecommendUserDto recommendUserDto){
        //查询该用户的所有推荐用户
        PageResult<TodayBest> pageResult = touchLoveService.getRecommendList(recommendUserDto);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 查看其他用户详细信息
     *
     * @param id id
     * @return {@link ResponseEntity}<{@link TodayBest}>
     */

    @GetMapping("/{id}/personalInfo")
    public ResponseEntity<TodayBest> personalInfo(@PathVariable Long id){
        //根据id查询用户详细信息
       TodayBest todayBest =  touchLoveService.findTodayBestById(id);
       log.info("获取到佳人详细信息:{}",todayBest.toString());
        return ResponseEntity.ok(todayBest);
    }

    @PostMapping("/strangerQuestions")
    public ResponseEntity answerStrangerQuestion(@RequestBody Map<String,Object> map){
        Integer userId = (Integer) map.get("userId");
        String questionReply = (String) map.get("reply");
        touchLoveService.answerStrangerQuestion(Long.valueOf(userId), questionReply);
        return ResponseEntity.ok("问题已答复!");
    }

    /**
     * 得到陌生人问题
     *
     * @param userId 用户id
     * @return {@link ResponseEntity}
     */
    @GetMapping("/strangerQuestions")
    public ResponseEntity getStrangerQuestion(Long userId){
        String question = touchLoveService.getStrangerQuestion(userId);
        return ResponseEntity.ok(question);
    }

}
